/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.zhihu.gkfb.main;

import cn.ac.iie.di.commons.httpserver.framework.server.HttpsServer;
import cn.ac.iie.di.zhihu.gkfb.common.RuntimeEnv;
import static cn.ac.iie.di.zhihu.gkfb.common.RuntimeEnv.writeJsonThreadSize;
import cn.ac.iie.di.zhihu.gkfb.configuration.Configuration;
import cn.ac.iie.di.zhihu.gkfb.handler.GKFeedBackHandler;
import cn.ac.iie.di.zhihu.gkfb.utils.FileListenerToYcl;
import cn.ac.iie.di.zhihu.gkfb.utils.WriteJson_new;
import java.io.IOException;
import java.io.InputStream;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.util.Timer;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.eclipse.jetty.util.thread.ExecutorThreadPool;

/**
 *
 * @author Administrator
 */
public class ZhihuQRMain {

    static HttpsServer httpServer = null;
    static Logger logger = Logger.getLogger(ZhihuQRMain.class.getName());

    public static void main(String[] args) {
        PropertyConfigurator.configure(ClassLoader.getSystemClassLoader().getResource("log4j.properties"));
        logger.info("log4j.properties has been configured");
        try {
            init();
            FileListenerToYcl fl = new FileListenerToYcl();
            Thread th = new Thread(fl);
            th.setName("FileListener");
            th.start();
            for (int i = 0; i < Integer.parseInt((String) RuntimeEnv.getParam(RuntimeEnv.writeJsonThreadSize)); i++) {
//                WriteJson wj = new WriteJson(i);
                WriteJson_new wj = new WriteJson_new(i);
                Thread wjt = new Thread(wj);
                wjt.setName("WriteJson - " + i);
                wjt.start();
            }
            startup();
            logger.info("server has been started .");
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }
    }

    public static void init() throws Exception {
        String configurationFileName = "configuration.properties";
        Configuration conf = Configuration.getConfiguration(ClassLoader.getSystemClassLoader().getResource(configurationFileName).getPath());
        if (conf == null) {
            logger.error("reading " + configurationFileName + " is failed.");
        }
        try {
            RuntimeEnv.initialize(conf);///参数，
            logger.info("init successfully");
        } catch (Exception ex) {
            logger.error("initializng runtime enviroment is failed for" + ex.getMessage());
        }
    }

    public static void startup() throws Exception {
        InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("zhihu_server.keystore");
        logger.info("ip " + RuntimeEnv.getParam(RuntimeEnv.JETTY_SERVER)
                + " ,port = " + Integer.parseInt((String) RuntimeEnv.getParam(RuntimeEnv.JETTY_PORT))
                + "  ,jettyThreadPoolSize = " + Integer.parseInt((String) RuntimeEnv.getParam(RuntimeEnv.JETTY_THREADNUM))
                + "  ,keyStorePassword" + (String) RuntimeEnv.getParam(RuntimeEnv.keyStorePassword));
        httpServer = new HttpsServer(
                (String) RuntimeEnv.getParam(RuntimeEnv.JETTY_SERVER),
                Integer.parseInt((String) RuntimeEnv.getParam(RuntimeEnv.JETTY_PORT)),
                Integer.parseInt((String) RuntimeEnv.getParam(RuntimeEnv.JETTY_THREADNUM)),
                is,
                (String) RuntimeEnv.getParam(RuntimeEnv.keyStorePassword),
                (String) RuntimeEnv.getParam(RuntimeEnv.keyManagerPassword));
        httpServer.init();
        httpServer.setTrustStore(ClassLoader.getSystemResourceAsStream("zhihu_client_pub.keystore"), (String) RuntimeEnv.getParam(RuntimeEnv.keyStorePassword));
        httpServer.registerContext("/push/mp/msg");
//        server.registerContext("/hello");
        httpServer.registerContextHandler("/push/mp/msg", "/", GKFeedBackHandler.class);
        httpServer.getNativeJettyServer().setThreadPool(new ExecutorThreadPool(256, 512, 10, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(4096)));
        logger.info("starting data dispatch server...");
        httpServer.startup();
        logger.info("start data dispatch server successfully");
        httpServer.join();
    }
}
